/************************************************************
**                                                         **
** FILE: Standard_functions.c                              **
**                                                         **
** DESCRIPTION:Standard functions for C programming        **
**                                                         **
** VERSION: 1.0                                            **
**                                                         **
** AUTOR: UTCH-RAPTOR TEAM                                 **
**                                                         **
** DATE: 26/September/2012                                 **
**                                                         **
************************************************************/

#include "PAD_Defs.h"
#include "Constant_Defs.h"
#include "MPC5604B_M27V.h"
#include "Variable_Declaration.h"

/*********************************************
Delay function of "ms_Counter" milliseconds **
*********************************************/
void Delay_ms(vuint32_t ms_Counter)
{
    vuint8_t ms_Incrementer;
	while(ms_Counter)
	{
		for(ms_Incrementer=0;ms_Incrementer<=8000;ms_Incrementer++)
		{}
		ms_Counter--;
	}
}
/*********************************************
Delay function of "us_Counter" microseconds **
*********************************************/
void Delay_us(vuint32_t us_Counter)
{
    vuint8_t us_Incrementer;
	while(us_Counter)
	{
		for(us_Incrementer=0;us_Incrementer<=8;us_Incrementer++)
		{}
		us_Counter--;
	}
}
/*********************************************
Function to toggle a variable, returns      **
the inverse value of the argument           **
"Toogle_variable"                           **
*********************************************/
vuint32_t Toggle_value(vuint32_t Toggle_variable)
{
	if(Toggle_variable ==1) Toggle_variable=0;
	else if(Toggle_variable==0) Toggle_variable=1;
	return Toggle_variable;
}
/*********************************************
Function to display a number using the LED's**
a decimal value is converted in a binary    **
variable                                    **
*********************************************/
void Binary_Display(vuint32_t Dec_Number)
{
	union
	   {
	      vuint32_t Number_to_Display;
	      struct
	         {
	            vuint32_t Binary_1:1;
	            vuint32_t Binary_2:1;
	            vuint32_t Binary_3:1;
	            vuint32_t Binary_4:1;           
	         }LED;
		
	   }DISPLAY_NUM;
	   DISPLAY_NUM.Number_to_Display=Dec_Number;
	LED_1=!DISPLAY_NUM.LED.Binary_1;
	LED_2=!DISPLAY_NUM.LED.Binary_2;
	LED_3=!DISPLAY_NUM.LED.Binary_3;
	LED_4=!DISPLAY_NUM.LED.Binary_4;
}
/*********************************************
Function to select a number with the push   **
buttons and display this number in the      **
binary diplay, returns selected number:     **
PUSH_1 for increment                        ** 
PUSH_2 for decrement                        **
PUSH_3 for finishes                         **
*********************************************/
vuint32_t Set_Number(vuint32_t Delay_in_ms)
{
    vuint32_t Selected_Number=0;
	while(PUSH_1==HIGHT_STATE)
	{
		if(PUSH_1==LOW_STATE)
		  { 
			 if(Selected_Number<=14)
			    {
			 	   Selected_Number++;
			    }
		  }
		if(PUSH_2==LOW_STATE)
		  { 
			 if(Selected_Number>=1)
			    {
			 	   Selected_Number--;
			    }
		  }	
	    Binary_Display(Selected_Number);
	    Delay_ms(Delay_in_ms);	  
	}
	return Selected_Number;
}

